package com.itmk.web.department.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itmk.utils.ResultUtils;
import com.itmk.utils.ResultVo;
import com.itmk.web.department.entity.Department;
import com.itmk.web.department.entity.DepartmentPageParm;
import com.itmk.web.department.entity.SelectDept;
import com.itmk.web.department.service.DepartmentService;
import com.itmk.web.sys_user.entity.SysUser;
import com.itmk.web.sys_user.entity.SysUserPage;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/api/department")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;



    @PreAuthorize("hasAuthority('sys:department:add')")
    @PostMapping("/add")
    public ResultVo add(@RequestBody Department department){
        if(departmentService.save(department)) {
            return ResultUtils.success("新增成功");
        }
        return ResultUtils.success("新增失败");
    }

    //编辑
    @PreAuthorize("hasAuthority('sys:department:edit')")
    @PutMapping("/edit")
    public ResultVo edit(@RequestBody Department department){
        if(departmentService.updateById(department)) {
            return ResultUtils.success("编辑成功");
        }
        return ResultUtils.success("编辑失败");

    }

    //删除
    @PreAuthorize("hasAuthority('sys:department:delete')")
    @DeleteMapping("/{deptId}")
    public ResultVo delete(@PathVariable("deptId") Long deptId ){
        if(departmentService.removeById(deptId)) {
            return ResultUtils.success("删除成功");
        }
        return ResultUtils.success("删除失败");


    }

    //列表
    @GetMapping("/list")
    //列表
    public ResultVo list(DepartmentPageParm parm){
        //构造分页对象
        IPage<Department> page = new Page<>(parm.getCurrentPage(),parm.getPageSize());
        //构造查询条件
        QueryWrapper<Department> query = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(parm.getDeptName())){
            query.lambda().like(Department::getDeptName,parm.getDeptName());
        }

        //排序，新的新出
        query.lambda().orderByDesc(Department::getOrderNum);
        IPage<Department> list =departmentService.page(page,query);
        return ResultUtils.success("查询成功",list);
    }
    //查询科室下拉数据
    @GetMapping("/getSelectDept")
    public ResultVo getSelectDept(){
        QueryWrapper<Department> query = new QueryWrapper<>();
        query.lambda().orderByDesc(Department::getOrderNum);
        List<Department> list = departmentService.list(query);
        List<SelectDept> deptList = new ArrayList<>();
        if(list.size()>0){
            for(int i=0;i<list.size();i++){
                SelectDept dept = new SelectDept();
                dept.setLabel(list.get(i).getDeptName());
                dept.setValue(list.get(i).getDeptId());
                deptList.add(dept);
            }
        }
        return ResultUtils.success("查询成功",deptList);
    }
}
